<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var now = new Date();
      console.log(now);
      // 注意new Date() 中要输入自定义的事件,需要遵循的格式
      var time = new Date("2021/03/16 09:09:04");
      console.log(time);
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var day = now.getDate();
      var hours = now.getHours();
      var minutes = now.getMinutes();
      var seconds = now.getSeconds();

      //前置补零
      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      hours = hours < 10 ? "0" + hours : hours;
      minutes = minutes < 10 ? "0" + minutes : minutes;
      seconds = seconds < 10 ? "0" + seconds : seconds;

      // alert(`${year}/${month}/${day} ${hours}:${minutes}:${seconds}`)
      // 获取今天是周几 如果直接获取系统时间，不需要其他转换，直接使用new Date就可以
      let week = "星期" + "日一二三四五六".charAt(now.getDay());
      console.log("今天是周几", week);
      // 获取年月日
      let dataDay = year + "." + month + "." + day;
      console.log("今天的日期", dataDay);
      // 获取时分秒
      let dateHour = hours + ":" + minutes + ":" + seconds;
      console.log("时分秒", dateHour);
    </script>
  </body>
</html>
